﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Network;
using OnlineGame;
using System.Diagnostics;

namespace OnlineGame_Server
{
    public class Program
    {
        private static ServerManager Server;
        private static PacketWriter PktWriter;
        private static PacketReader PktReader;

        private static void Main(string[] args)
        {
            Console.WriteLine("Welcome to the Game's server.");

            Thread thrdInput = new Thread(new ThreadStart(InputManager));
            thrdInput.Start();
            
            for (int i = 0; i < 2; i++)
            {
                Process.Start(@"C:\Users\Topaz\Documents\Visual Studio 2012\Projects\OnlineGame\OnlineGame\bin\Debug\OnlineGame.exe");
            }
        }

        private static void InputManager()
        {
            while (true)
            {
                string input = Console.ReadLine();
                string cmd = input.Split(' ')[0];

                switch (cmd.ToLower())
                {
                    case "/startserver":

                        string[] args = input.Split(' ');

                        if ((args.Length == 2) && (args[1].Length > 0))
                        {
                            int port = int.Parse(args[1]);
                            Server = new ServerManager(port);
                            Server.Start();
                            Server.OnClientJoin += Server_OnClientJoin;
                            Server.OnReceiveData += Server_OnReceiveData;
                        }
                        else
                        {
                            int port = 7777;
                            Server = new ServerManager(port);
                            Server.Start();
                            Server.OnClientJoin += Server_OnClientJoin;
                            Server.OnReceiveData += Server_OnReceiveData;
                        }

                        break;

                    case "/stop":

                        Server.Stop();
                        break;

                    case "/exit":

                        Server.Stop();
                        return;

                    default:

                        Server.SendMessageToAllClient(input);
                        break;
                }
            }
        }

        private static void Server_OnClientJoin(int clientid)
        {
            PktWriter = new PacketWriter();
            PktWriter.Write((ushort)Headers.PlayerJoin);
            PktWriter.Write(Server.Clients.Count);
            PktWriter.Write(clientid);
            Server.SendDataToAllClient(PktWriter.GetBytes());
        }

        private static void Server_OnReceiveData(int clientid, byte[] data)
        {
            PktReader = new PacketReader(data);
            Headers headers = (Headers)PktReader.ReadUInt16();

            switch (headers)
            {
                case Headers.Text:

                    string text = PktReader.ReadString();
                    Console.WriteLine("CLIENT " + clientid.ToString() + ": " + text);
                    break;

                case Headers.PlayerData:

                    Player player = PktReader.ReadOjbect<Player>();
                    Console.WriteLine("Player Name: " + player.Name);
                    Console.WriteLine("Player Health: " + player.Health);
                    Console.WriteLine("Player Lives: " + player.Lives);
                    Server.SendDataToAllClientExcept(clientid, data);
                    break;

                case Headers.GameStarted:

                    PktWriter = new PacketWriter();
                    PktWriter.Write((ushort)Headers.GameStarted);
                    PktWriter.Write(clientid);
                    Server.SendDataToAllClientExcept(clientid, PktWriter.GetBytes());
                    Console.WriteLine("The game has been started by client ID " + clientid);
                    break;
            }
        }
    }
}
